/*
 * EditorLayer.java - Copyright Apr 16, 2012 Z Networks Group, Inc. All rights reserved.
 * Z Networks Group PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.andromeda.skadi.view.swing;

import com.andromeda.skadi.property.EditableProperty;
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.plaf.BufferedLayerUI;

/**
 *
 * @author Dickinson Roman Arismendy Torres
 */
public class EditorLayer<T extends JComponent> extends BufferedLayerUI<T> {

    private int iconW = 10;
    private int iconH = 10;
    private EditableProperty<Boolean> selected = new EditableProperty<Boolean>(Boolean.TRUE);
    private Button securityButton = new Button();
    private Button lockButton = new Button();
    private Button helpButton = new Button();
    private Button validationButton = new Button();
    private Button commandButton = new Button();

    public EditorLayer() {
    }

    public EditableProperty<Boolean> selected() {
        return selected;
    }

    @Override
    protected void paintLayer(Graphics2D g2, JXLayer<? extends T> l) {
        super.paintLayer(g2, l);
        if (selected.get()) {
            g2 = (Graphics2D)g2.create();
            g2.setColor(l.getBackground().darker());
            g2.drawRoundRect(2, 2, l.getWidth() - 5, l.getHeight() - 5, 1, 1);
            int width = l.getWidth();
            int x = width - (iconW * 4);
            int y = 0;
            drawSecurity(g2, x, y, iconW, iconH);
            x = iconW + x;
            drawLock(g2, x, y, iconW, iconH);
            x = iconW + x;
            drawValidation(g2, x, y, iconW, iconH);
            x = iconW + x;
            drawHelp(g2, x, y, iconW, iconH);
            y = y + iconH;
            drawCommand(g2, x, y, iconW, iconH);
            g2.dispose();
        }
    }
    

    private void drawSecurity(Graphics g, int x, int y, int w, int h) {
        drawRect(g, x, y, w, h, Color.red);
        securityButton.insets = new Insets(x, y, x + w, y + h);
    }

    private void drawLock(Graphics g, int x, int y, int w, int h) {
        drawRect(g, x, y, w, h, Color.blue);
        lockButton.insets = new Insets(x, y, x + w, y + h);
    }

    private void drawHelp(Graphics g, int x, int y, int w, int h) {
        drawRect(g, x, y, w, h, Color.yellow);
        helpButton.insets = new Insets(x, y, x + w, y + h);
    }

    private void drawValidation(Graphics g, int x, int y, int w, int h) {
        drawRect(g, x, y, w, h, Color.green);
        validationButton.insets = new Insets(x, y, x + w, y + h);
    }

    private void drawCommand(Graphics g, int x, int y, int w, int h) {
        drawRect(g, x, y, w, h, Color.PINK);
        commandButton.insets = new Insets(x, y, x + w, y + h);
    }

    private void drawRect(Graphics g, int x, int y, int w, int h, Color c) {
        g = g.create();
        g.setColor(c);
        g.fillRect(x, y, w, h);
        g.dispose();
    }

    private class Button {

        private Insets insets;
    }

    private static boolean containsPoint(Insets insets, int x, int y) {
        return (insets.left < x && insets.right > x) && (insets.top < y && insets.bottom > y);
    }

    @Override
    protected void processMouseEvent(MouseEvent e, JXLayer<? extends T> l) {
        if (e.getID() == MouseEvent.MOUSE_PRESSED) {
            selected.set(!selected.get());
            System.out.println("x: " + e.getX() + " - y: " + e.getY());
            if(containsPoint(securityButton.insets, e.getX(), e.getY())){
                l.setCursor(Cursor.getDefaultCursor());
            }
        }
        l.repaint();
    }
}
